import { Controller } from "../../mvc/core/Controller";
import { CrossMainCityIcon } from "../view/crossMainCity/CrossMainCityIcon";
import { Dispatcher } from "../../mvc/core/Dispatcher";
import { EventName } from "../mvc/EventName";
import { RolePlayer } from "../scene/player/entity/RolePlayer";
import { PlayerEvent } from "../scene/events/PlayerEvent";
import { NetDispatcher } from "../../mvc/core/NetDispatcher";
import { EPublicCommand } from "../../../Message/Command/EPublicCommand";
import { GameMapUtil } from "../scene/map/GameMapUtil";
import { DataEvent } from "../events/DataEvent";
import { MessageBlock } from "../../../Framework/MQ/MessageBlock";
import { SCrossUniqueOpen } from "../../../Message/Public/SCrossUniqueOpen";
import { ClockManager } from "../manager/ClockManager";
import { GameProxy } from "../mvc/GameProxy";
import { EPassType } from "../../../Message/Game/EPassType";
import { MsgManager } from "../manager/MsgManager";
import { Language } from "../../../extend/language/Language";
import { SSTBelongToInfo } from "../../../Message/Public/SSTBelongToInfo";
import { ServerCommand } from "../mvc/ServerCommand";
import { ThingUtil } from "../scene/layer/utils/ThingUtil";
//class CrossMainCityController
    
    export  class CrossMainCityController extends Controller
    {
       

        public get icon(): CrossMainCityIcon
        {
            if (this._icon == null) 
            {
                this._icon = new CrossMainCityIcon();
                this._icon.addEventListener(flash.events.MouseEvent.CLICK, this.onIconClickHandler);
            }
            return this._icon;
        }

        protected /* override */ initServer(): void
        {
            Dispatcher.addEventListener(EventName.Scene_Update, this.onSceneUpdateHandler);
            RolePlayer.instance.addEventListener(PlayerEvent.RoleLevelUpdate, this.onRoleLevelUpdate);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossUniqueOpen, this.onCrossUniqueOpen);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossUniqueClose, this.onCrossUniqueClose);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicCrossUniqueOpenNotice, this.onCrossUniqueOpenNotice);
            NetDispatcher.addCmdListener(EPublicCommand._ECmdPublicSTBelongToInfoBack, this.onReceiveSTBelongToInfoHandler);
            return;
        }

        /* internal  */onSceneUpdateHandler(arg1): void
        {
            if (GameMapUtil.curMapState.isCrossMainCity) 
            {
                this.hideIcon();
                this._isLastCrossMainCity = true;
                Dispatcher.dispatchEvent(new DataEvent(EventName.CopyAddLeaveButton));
                Dispatcher.dispatchEvent(new DataEvent(EventName.CrossMainCity_Enter));
            }
            else 
            {
                if (cache.crossMainCity.isInCrossMainCityActive) 
                {
                    this.showIcon();
                }
                if (this._isLastCrossMainCity) 
                {
                    this._isLastCrossMainCity = false;
                    Dispatcher.dispatchEvent(new DataEvent(EventName.CrossMainCity_Leave));
                    if (!cache.copy.isInCopy) 
                    {
                        Dispatcher.dispatchEvent(new DataEvent(EventName.CopyRemoveLeaveButton));
                    }
                }
            }
            return;
        }

        /* internal  */onRoleLevelUpdate(arg1): void
        {
            if (cache.crossMainCity.isInCrossMainCityActive && cache.role.roleInfo.level >= 70) 
            {
                this.showIcon();
            }
            return;
        }

        /* internal  */hideIcon(): void
        {
            if (this._icon && !this._icon.isHide) 
            {
                this._icon.hide();
            }
            return;
        }

        /* internal  */showIcon(): void
        {
            if (cache.role.roleInfo.level < 70) 
            {
                return;
            }
            if (GameMapUtil.curMapState.isCrossMainCity) 
            {
                return;
            }
            this.icon.show();
            return;
        }

        /* internal  */onCrossUniqueOpen(arg1: MessageBlock): void
        {
            cache.crossMainCity.isInCrossMainCityActive = true;
            this.showIcon();
            this.icon.disposeCountDown();
            return;
        }

        /* internal  */onCrossUniqueClose(arg1: MessageBlock): void
        {
            cache.crossMainCity.isInCrossMainCityActive = false;
            this.hideIcon();
            return;
        }

        /* internal  */onCrossUniqueOpenNotice(arg1: MessageBlock): void
        {
            if (cache.role.roleInfo.level < 70) 
            {
                return;
            }
            var loc1=arg1.messageBase as SCrossUniqueOpen;
            this.icon.setCountDown(Math.ceil((loc1.openDt.time - ClockManager.instance.nowDate.time) / 1000));
            return;
        }

        /* internal  */onIconClickHandler(arg1: Object /* flash.events.MouseEvent */): void
        {
            if (cache.crossMainCity.isInCrossMainCityActive) 
            {
                GameProxy.sceneProxy.pass(EPassType.convert(EPassType._EPassTypeCrossUnique), 1, 0);
            }
            else 
            {
                MsgManager.showRollTipsMsg(Language.getString(60900));
            }
            return;
        }

        /* internal  */onReceiveSTBelongToInfoHandler(arg1: MessageBlock): void
        {
            var loc1=arg1.messageBase as SSTBelongToInfo;
            cache.crossMainCity.ownerInfo = loc1;
            NetDispatcher.dispatchCmd(ServerCommand.CrossMainCity_BelongInfoChange, loc1);
            ThingUtil.npcUtil.updateAllNpcName();
            return;
        }

        private /* var */_icon: CrossMainCityIcon;

        private /* var */_isLastCrossMainCity: Boolean;
    }
